import 'package:dio/dio.dart';
import 'package:flutter_reader/model/me/consum_model.dart';
import 'package:flutter_reader/model/me/exchange_config_model.dart';
import 'package:flutter_reader/model/me/exchange_result_model.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:flutter_reader/tools/globals.dart' as globals;

 String BSAuthorization = globals.fullSessionID;
const BaseURL = 'http://appapi.98nice.cn/api';
const testURL = 'http://appapi.98nice.cn/api';

class MeDao{
  static Future<ConsumModel> fetchConsumData() async{
    final response = await http.get(BaseURL+'/record/consume?page=1'
        ,headers: {'BSAuthorization':globals.fullSessionID,'READING':'API'});
    if(response.statusCode == 200){
      //解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      final json = jsonDecode(utf8decoder.convert(response.bodyBytes));
      return ConsumModel.fromJson(json);
    }
    else
    {
      throw Exception('加载consum接口失败');
    }
  }

  static Future<ConsumModel> fetchRechargeData() async{
    final response = await http.get(BaseURL+'/record/charge?page=1'
        ,headers: {'BSAuthorization':globals.fullSessionID,'READING':'API'});
    if(response.statusCode == 200){
      //解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      final json = jsonDecode(utf8decoder.convert(response.bodyBytes));
      return ConsumModel.fromJson(json);
    }
    else
    {
      throw Exception('加载recharge接口失败');
    }
  }

  static Future<ExchangeConfigModel> fetchExchangeConfig() async{
    final response = await http.get(BaseURL+'/config/exchange'
        ,headers: {'BSAuthorization':globals.fullSessionID,'READING':'API'});
    if(response.statusCode == 200){
      //解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      final json = jsonDecode(utf8decoder.convert(response.bodyBytes));
      return ExchangeConfigModel.fromJson(json);
    }
    else
    {
      throw Exception('加载exchangeconfig接口失败');
    }
  }

  static Future exchange(String type) async {
    Dio dio = Dio();
    final response = await dio.get(
        BaseURL + '/user/exchange?prod=${type}&amount=1',
        options: Options(
            headers: {
              'BSAuthorization': globals.fullSessionID,
              'READING': 'API'
            }));
    if (response.statusCode == 200) {
      print(response.data['data']['coin']);
      return response.data;
    }
    else {
      print(response.statusCode);
      throw Exception('加载callback接口失败');
    }
  }

  static Future sendCode(int phoneNum) async{
    print('--------- 验证码获取 --------------');
    Dio dio = Dio();
    print(BaseURL + '/user/sms/send?mobile=${phoneNum}');
    final response = await dio.get(
      BaseURL + '/user/sms/send?mobile=${phoneNum}',
      options: Options(
        headers: {
          'BSAuthorization': globals.fullSessionID,
          'READING': 'API'
        }
      )
    );
    if(response.statusCode == 200)
    {
      return response.data;
    }
    else
    {
      print(response.statusCode);
      throw Exception('加载sendCode接口失败');
    }
  }

  static Future bindPhone(int phoneNum,int code) async{
    Dio dio = Dio();
    print("绑定的sessionID:${BSAuthorization}");
    final response = await dio.get(
      BaseURL + '/user/bind/tel?mobile=${phoneNum}&code=${code}',
      options: Options(
        headers:{
          'BSAuthorization': globals.fullSessionID,
          'READING': 'API'
        }
      )
    );
    if(response.statusCode == 200)
      {
        return response.data;
      }
    else
      {
        print(response.statusCode);
        throw Exception('加载bind接口失败');
      }
  }

  static Future checkVersion() async{
    Dio dio = Dio();
    final response = await dio.get(BaseURL+"/app/update?appid=${globals.appID}&ver=0");
    if(response.statusCode == 200)
      {
        return response.data;
      }
    else
      {
        print(response.statusCode);
        throw Exception("加载checkVersion接口失败");
      }
  }
}